﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Maintenance.Util
{
    /// <summary>
    /// 获取或设置当前应用程序的配置文件，本应用程序的所有的功能都配置到当前配置文件application.ini下
    /// </summary>
    public class AppConfigure
    {
        /// <summary>
        /// 将所有的Ini的文件配置，同步到当前的字典中,主字典的键为配置的Section的名称，值为每个配置项的配置
        /// 子字典为每个配置的键和值
        /// </summary>
        public Dictionary<string, Dictionary<string, string>> CurrentConfig { get; private set; }

        /// <summary>
        /// 配置文件，放在与当前dll相同的目录下面
        /// </summary>
        private string _appFile;

        private IniFileUtil initFile;

        /// <summary>
        /// 应用程序的主目录，即exe所在目录
        /// </summary>
        public string AppPath { set; get; }

        public AppConfigure()
        {
            _appFile = Path.Combine(DirectoryUtil.AssemblyDirectory(), "application.ini");
            AppPath = DirectoryUtil.AssemblyDirectory();
            initFile = new IniFileUtil(_appFile);
            //初始化系统的配置
            CurrentConfig = new Dictionary<string, Dictionary<string, string>>();
            LoadIni();
        }

        public void LoadIni()
        {
            CurrentConfig.Clear();
            //获取所有的Section的名称
            List<string> sectionList = initFile.ReadSections();
            sectionList.ForEach(p =>
            {
                List<string> keysList = initFile.ReadKeys(p);
                Dictionary<string, string> items = new Dictionary<string, string>();
                keysList.ForEach(x =>
                {
                    items.Add(x, initFile.IniReadValue(p, x));
                });
                CurrentConfig.Add(p, items);
            });
        }

        /// <summary>
        /// 获取指定的Config的值
        /// </summary>
        /// <param name="configureName"></param>
        /// <returns></returns>
        public string GetConfigureValue(string section, string configureName, string defaultValue = "")
        {
            return initFile.IniReadValue(section, configureName, defaultValue);
        }

        /// <summary>
        /// 获取缓存的配置文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="configureName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string GetCacheConfigureValue(string section, string configureName, string defaultValue = "")
        {
            if (!CurrentConfig.ContainsKey(section))
            {
                throw new Exception(string.Format("配置文件中，未找到名称为{0}的Section节点", Constants.SECTION_APPLICATION));
            }
            if (!CurrentConfig[Constants.SECTION_APPLICATION].ContainsKey(configureName))
            {
                return defaultValue;
            }
            return CurrentConfig[Constants.SECTION_APPLICATION][configureName];
        }

        /// <summary>
        /// 保存某个配置项
        /// </summary>
        /// <param name="section"></param>
        /// <param name="configureName"></param>
        /// <param name="configureValue"></param>
        /// <returns></returns>
        public void SaveConfigureItem(string section, string configureName, string configureValue)
        {
            initFile.IniWriteValue(section, configureName, configureValue);
        }

        /// <summary>
        /// 获取系统配置节点下的项
        /// </summary>
        /// <returns></returns>
        public string GetSysConfig(string pKey, string defaultValue = "")
        {
            if (!CurrentConfig.ContainsKey(Constants.SECTION_APPLICATION))
            {
                throw new Exception(string.Format("配置文件中，未找到名称为{0}的Section节点", Constants.SECTION_APPLICATION));
            }
            if (!CurrentConfig[Constants.SECTION_APPLICATION].ContainsKey(pKey))
            {
                return defaultValue;
            }
            return CurrentConfig[Constants.SECTION_APPLICATION][pKey];
        }

        /// <summary>
        /// 修改系统配置文件
        /// </summary>
        /// <param name="pKey"></param>
        /// <param name="pValue"></param>
        public void SetSysConfig(string pKey, string pValue, bool save = true)
        {
            if (!CurrentConfig.ContainsKey(Constants.SECTION_APPLICATION))
            {
                CurrentConfig.Add(Constants.SECTION_APPLICATION, new Dictionary<string, string>() { { pKey, pValue } });
            }
            else
            {
                if (!CurrentConfig[Constants.SECTION_APPLICATION].ContainsKey(pKey))
                {
                    CurrentConfig[Constants.SECTION_APPLICATION].Add(pKey, pValue);
                }
                else
                {
                    CurrentConfig[Constants.SECTION_APPLICATION][pKey] = pValue;
                }
            }

            if (save)
            {
                initFile.IniWriteValue(Constants.SECTION_APPLICATION, pKey, pValue);
            }
        }
    }
}